import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/test',
      name: 'test',
      component: () => import('../views/TestView.vue')
    },
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/category',
      name: 'category',
      component: () => import('../views/CategoryView.vue')
    },
    {
      path: '/product',
      name: 'product',
      component: () => import('../views/ProductView.vue')
    },
    {
      path: '/productDetail',
      name: 'productDetail',
      component: () => import('../views/ProductDetail.vue')
    },
    {
      path: '/discounts',
      name: 'discounts',
      component: () => import('../views/DiscountsView.vue')
    },
    {
      path: '/promote',
      name: 'promote',
      component: () => import('../views/PromoteView.vue')
    },
    {
      path: '/credit',
      name: 'credit',
      component: () => import('../views/CreditView.vue')
    },
    {
      path: '/shippingPolicy',
      name: 'shippingPolicy',
      component: () => import('../views/PolicyShipping.vue')
    },
    {
      path: '/returnPolicy',
      name: 'returnPolicy',
      component: () => import('../views/PolicyReturn.vue')
    },
    {
      path: '/delivery',
      name: 'delivery',
      component: () => import('../views/DeliveryView.vue')
    },
    {
      path: '/privacyPolicy',
      name: 'privacyPolicy',
      component: () => import('../views/PolicyPrivacy.vue')
    },
    {
      path: '/store',
      name: 'store',
      component: () => import('../views/StoreView.vue')
    },
    {
      path: '/mine',
      name: 'mine',
      component: () => import('../views/mine/index.vue'),
      redirect: '/mine/dashboard',
      children: [
        {
          path: 'dashboard',
          component: () => import('@/views/mine/Dashboard.vue')
        },
        {
          path: 'money-package',
          component: () => import('@/views/mine/MoneyPackage.vue'),
          redirect: '/mine/money-package',
          children: [
            {
              path: '',
              component: () => import('@/views/mine/package/index.vue'),
            },
            {
              path: 'recharge',
              component: () => import('@/views/mine/package/Recharge.vue')
            },
            {
              path: 'withdraw',
              component: () => import('@/views/mine/package/Withdraw.vue')
            }
          ]
        },
        {
          path: 'my-order',
          component: () => import('@/views/mine/MyOrder.vue'),
        },
        {
          path: 'order-detail',
          component: () => import('@/views/mine/OrderDetail.vue'),
        },
        {
          path: 'collect-goods',
          component: () => import('@/views/mine/CollectGoods.vue')
        },
        {
          path: 'collect-shop',
          component: () => import('@/views/mine/CollectShop.vue')
        },
        {
          path: 'setup',
          component: () => import('@/views/mine/Setup.vue'),
          redirect: '/mine/setup',
          children: [
            {
              path: '',
              component: () => import('@/views/mine/setup/index.vue')
            },
            {
              path: 'login-password',
              component: () => import('@/views/mine/setup/LoginPassword.vue')
            },
            {
              path: 'transaction-password',
              component: () => import('@/views/mine/setup/TransactionPassword.vue')
            },
            {
              path: 'shipping-address',
              component: () => import('@/views/mine/setup/ShippingAddress.vue')
            },
            {
              path: 'account-cancellation',
              component: () => import('@/views/mine/setup/AccountCancellation.vue')
            }
          ]
        }
      ]
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0 }
  },
})
router.beforeEach((to, from, next) => {
  // 读取数据
  let token = sessionStorage.getItem('token');
  if(token){
    next();
  }else{
    const authReg = /mine/
    if(authReg.test(to.path)) {
      if(from.path === '/login') {
        next()
      }else{
        router.push('/login')
      }
    }else{
      next()
    }
  }
})

export default router
